package com.fastfood.model.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.fastfood.model.dto.Food;
import com.fastfood.model.dto.FoodType;

public class FoodDAO {

	public boolean addFood(Connection conn, Food food) {
		String query = "insert into foods (name, description, price, type_id, image, available) values (?, ?, ?, ?, ?, ?)";
		boolean result = false;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, food.getName());
			preparedStatement.setString(2, food.getDescription());
			preparedStatement.setDouble(3, food.getPrice());
			preparedStatement.setInt(4, food.getType().getId());
			preparedStatement.setString(5, food.getImage());
			preparedStatement.setInt(6, food.getAvailable());
			preparedStatement.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}

	public boolean deleteFood(Connection conn, Food food) {
		String query = "delete from foods where id = ?";
		boolean result = false;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setInt(1, food.getId());
			preparedStatement.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public boolean updateFood(Connection conn, Food food) {
		String query = "update foods set name = ?, description = ?, price = ?," 
					+ " type_id = ?, image = ?, available = ? where id = ?";
		boolean result = false;
		try {
			PreparedStatement preparedStatement = conn.prepareStatement(query);
			preparedStatement.setString(1, food.getName());
			preparedStatement.setString(2, food.getDescription());
			preparedStatement.setDouble(3, food.getPrice());
			preparedStatement.setInt(4, food.getType().getId());
			preparedStatement.setString(5, food.getImage());
			preparedStatement.setInt(6, food.getAvailable());
			preparedStatement.setInt(7, food.getId());
			preparedStatement.executeUpdate();
			result = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	public Food getFoodById(Connection conn, int id) {
		Food food = null;
		try {
			ResultSet result = conn.createStatement().executeQuery("select * from foods where id = " + id);
			if (result.next()) {
				FoodTypeDAO dao = new FoodTypeDAO();
				FoodType temp = dao.getFoodTypeById(conn, result.getInt("type_id"));
				
				if (temp != null) {
					food = new Food();
					food.setId(result.getInt("id"));
					food.setName(result.getString("name"));
					food.setDescription(result.getString("description"));
					food.setPrice(result.getFloat("price"));
					food.setType(temp);
					food.setImage(result.getString("image"));
					food.setAvailable(result.getInt("available"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return food;
	}
	
	public List<Food> getAllFoods(Connection conn) {
		List<Food> foods = null;
		try {
			ResultSet result = conn.createStatement().executeQuery("select * from foods");
			foods = new ArrayList<Food>();
			while (result.next()) {
				Food food = new Food();
				FoodTypeDAO dao = new FoodTypeDAO();
				FoodType temp = dao.getFoodTypeById(conn, result.getInt("type_id"));
				
				if (temp != null) {
					food = new Food();
					food.setId(result.getInt("id"));
					food.setName(result.getString("name"));
					food.setDescription(result.getString("description"));
					food.setPrice(result.getFloat("price"));
					food.setType(temp);
					food.setImage(result.getString("image"));
					food.setAvailable(result.getInt("available"));
					
					foods.add(food);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return foods;
	}
}
